package njust.cms.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import njust.cms.sql.Hql;
import njust.tables.boardroom.TbBoardroom;
import njust.tables.boardroom.TbBoardroomDAO;
import njust.tables.order.TbOrder;
import njust.tables.order.TbOrderDAO;
import njust.tables.student.TbStudent;
import njust.tables.student.TbStudentDAO;
import njust.tables.teacher.TbTeacher;
import njust.tables.teacher.TbTeacherDAO;
import njust.tables.time.TbTime;
import njust.tables.time.TbTimeDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/room")
public class Room {

	@Resource(name = "TbBoardroomDAO")
	private TbBoardroomDAO boradroomdao;

	@Resource(name = "TbStudentDAO")
	private TbStudentDAO studentdao;

	@Resource(name = "TbTeacherDAO")
	private TbTeacherDAO teacherdao;

	@Resource(name = "TbTimeDAO")
	private TbTimeDAO timedao;

	@Resource(name = "TbOrderDAO")
	private TbOrderDAO orderdao;

	@RequestMapping("/book")
	public String bookroom(HttpServletRequest request) {

		Date[] date = new Date[8];
		String[] dateShow = new String[7];
		date[0] = new Date(); // 当前日期
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < date.length - 1; i++) {
			calendar.setTime(date[i]); // 当前日期加入日历
			calendar.add(Calendar.DAY_OF_MONTH, +1);
			date[i + 1] = calendar.getTime(); // 取得后一天的日期
		}
		for (int i = 0; i < dateShow.length; i++) {
			dateShow[i] = (new SimpleDateFormat("yyyy-MM-dd"))
					.format(date[i + 1]);
		}

		List<TbTime> timelist = timedao.findAll();
		List<TbBoardroom> roomlist = boradroomdao.findAll();

		request.setAttribute("roomlist", roomlist);
		request.setAttribute("datelist", dateShow);
		request.setAttribute("timelist", timelist);
		return "room_book";
	}

	@RequestMapping("/bookbytime")
	public String bookbytime(HttpServletRequest request, String date,
			String time) {
		String query1 = "from TbBoardroom room where room.BId not in (select model.BId from TbOrder as model where model.orderDate='"
				+ date
				+ "' and model.orderTime='"
				+ time
				+ "' and model.state<>2)";
		String query2 = "from TbBoardroom room where room.BId in (select model.BId from TbOrder as model where model.orderDate='"
				+ date
				+ "' and model.orderTime='"
				+ time
				+ "'  and model.state<>2)";
		List<TbBoardroom> orderlist = Hql.findByhql(query1);
		List<TbBoardroom> notorderlist = Hql.findByhql(query2);
		Date[] dates = new Date[8];
		String[] dateShow = new String[7];
		dates[0] = new Date(); // 当前日期
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < dates.length - 1; i++) {
			calendar.setTime(dates[i]); // 当前日期加入日历
			calendar.add(Calendar.DAY_OF_MONTH, +1);
			dates[i + 1] = calendar.getTime(); // 取得后一天的日期
		}
		for (int i = 0; i < dateShow.length; i++) {
			dateShow[i] = (new SimpleDateFormat("yyyy-MM-dd"))
					.format(dates[i + 1]);
		}

		List<TbTime> timelist = timedao.findAll();

		request.setAttribute("datelist", dateShow);
		request.setAttribute("timelist", timelist);
		request.setAttribute("orderlist", orderlist);
		request.setAttribute("notorderlist", notorderlist);

		request.setAttribute("date", date);
		request.setAttribute("timeid", time);
		return "room_bookbytime";
	}

	@RequestMapping("/bookbyroom")
	public String bookbyroom(HttpServletRequest request, String roomid) {
		String query = "from TbOrder as model where model.BId='" + roomid + "'";
		List<TbOrder> orderlist = orderdao.findByhql(query);
		String query1 = "from TbBoardroom as model where model.BId='" + roomid
				+ "'";
		List<TbBoardroom> room = Hql.findByhql(query1);
		Date[] dates = new Date[8];
		String[] dateShow = new String[7];

		dates[0] = new Date(); // 当前日期
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < dates.length - 1; i++) {
			calendar.setTime(dates[i]); // 当前日期加入日历
			calendar.add(Calendar.DAY_OF_MONTH, +1);
			dates[i + 1] = calendar.getTime(); // 取得后一天的日期
		}
		for (int i = 0; i < dateShow.length; i++) {
			dateShow[i] = (new SimpleDateFormat("yyyy-MM-dd"))
					.format(dates[i + 1]);
		}

		List<TbTime> timelist = timedao.findAll();

		request.setAttribute("datelist", dateShow);
		request.setAttribute("timelist", timelist);
		request.setAttribute("orderroom", orderlist);
		request.setAttribute("roomid", roomid);
		request.setAttribute("room", room.get(0));
		return "room_bookbyroom";
	}

	@RequestMapping("/saveroom")
	public String saveroom(HttpServletRequest request, String roomid,
			String date, String time) {
		String query1 = "from TbBoardroom as model where model.BId='" + roomid
				+ "'";
		List<TbBoardroom> room = Hql.findByhql(query1);
		Integer i = new Integer(time);
		TbTime times = timedao.findById(i);
		request.setAttribute("room", room.get(0));
		request.setAttribute("bookdate", date);
		request.setAttribute("booktime", times);
		return "room_saveroom";
	}

	@RequestMapping("/submitbookroom")
	public String submitbookroom(HttpServletRequest request, String roomid,
			String bookdate, String booktime, String reason)
			throws ParseException {
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("Id");
		String role = (String) session.getAttribute("Role");
		TbOrder order = new TbOrder();
		order.setBId(roomid);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
		String college = "";
		if (role.equals("student")) {
			TbStudent stu = (TbStudent) studentdao.findByUserId(userid).get(0);
			college = stu.getCollege();
		}
		if (role.equals("teacher")) {
			TbTeacher teac = (TbTeacher) teacherdao.findByUserId(userid).get(0);
			college = teac.getCollege();
		}

		Date date = sdf.parse(bookdate);
		order.setOrderDate(date);
		order.setOrderTime(new Integer(booktime));
		order.setReason(reason);
		order.setUserId(userid);
		order.setCollege(college);
		order.setApproved(0);
		order.setState(1);
		orderdao.save(order);
		return "redirect:book";
	}

}
